﻿<%@ WebHandler Language="C#" Class="SalePaymentsBalance" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using Chenxin.Lib.Business.BLL;
using Chenxin.Lib.Utility;

public class SalePaymentsBalance : AjaxHandlerBase
{

    //*******************************************
    //功能概要       ：基本权限初始化设定
    //参数説明       ： 
    //返回值説明     ：-
    //功能詳細       ：-
    //作成者         ：chen.yugong 
    //作成日         ：2010-06-29
    //修改者         ：
    //修改日         ：2010-07-13
    //*******************************************
    public SalePaymentsBalance()
    {
        //导出
        htTypePermit.Add("export", Constants.Permit_SalePaymentsBalance_Export);
        htTypePermit.Add("exportall", Constants.Permit_SalePaymentsBalance_Export);
    }
    
    //*******************************************
    //功能概要       ：请求处理
    //参数説明       ：context 请求上下文
    //返回值説明     ：-
    //功能詳細       ：根据请求名 分发请求
    //作成者         ：he.jifeng
    //作成日         ：2010-06-29
    //*******************************************
    override public void RequestProcess(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";

        string strRet = "";
        try
        {
            string strOp = context.Request["op"];
            switch (strOp.ToLower())
            {
                case "search":
                    strRet = GetBalanceList(context);
                    break;
                case "exportall":
                    ExportAll(context);
                    break;
                default:
                    break;
            }

            context.Response.Write(strRet);
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            context.Response.Write(strRet);
        }
    }

    //获取全部导出
    private void ExportAll(HttpContext context)
    {
        string strParam = string.Empty;
        Dictionary<string, string> dicParams = new Dictionary<string, string>();
        ContractBaseInfo bllBaseInfo = new ContractBaseInfo();

        strParam = context.Request.Params["pagesize"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("PageSize", strParam);
        }
        strParam = context.Request.Params["pageindex"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("PageIndex", strParam);
        }
        strParam = context.Request.Params["sort"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Sort", strParam);
        }
        strParam = context.Request.Params["StartTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("StartTime", strParam);
        }
        strParam = context.Request.Params["EndTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("EndTime", strParam);
        }
        strParam = context.Request.Params["Keyword"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Keyword", strParam);
        }
        strParam = context.Request.Params["BalanceStatus"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("BalanceStatus", strParam);
        }
        strParam = context.Request.Params["DateType"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("DateType", strParam);
        }

        if (!string.IsNullOrEmpty(context.Request.Params["departid"]))
        {
            dicParams.Add("DepartId", context.Request.Params["departid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["areaid"]))
        {
            dicParams.Add("AreaId", context.Request.Params["areaid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["companyid"]))
        {
            dicParams.Add("CompanyId", context.Request.Params["companyid"]);
        }
        if (!string.IsNullOrEmpty(context.Request.Params["cityid"]))
        {
            dicParams.Add("CityId", context.Request.Params["cityid"]);
        }
        else
        {
            dicParams.Add("CompanyId", CurrentUserInfo.CompanyId.ToString());
        }

        System.Data.DataSet dsBaseInfo = bllBaseInfo.GetSaleBalanceExportAll(dicParams);

        string strHTML = "";
        strHTML = GetHtml(dsBaseInfo.Tables[0]);

        ExportByServce(context, strHTML);
    }

    private string GetHtml(System.Data.DataTable dt)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("<table class=\"tabsorternew table_archive\"><tr>");
        sb.Append("<th style=\"width:120px;\">商圈</th>");
        sb.Append("<th style=\"width:120px;\">部门</th>");
        sb.Append("<th style=\"width:100px;\">业务员</th>");
        sb.Append("<th style=\"width:130px;\">成交编号</th>");
        sb.Append("<th style=\"width:80px;\">成交日期</th>");
        sb.Append("<th style=\"width:140px;\">上家客户姓名</th>");
        sb.Append("<th style=\"width:140px;\">下家客户姓名</th>");
        sb.Append("<th style=\"width:300px;\">房源地址</th>");
        sb.Append("<th style=\"width:80px;\">建筑面积</th>");
        sb.Append("<th style=\"width:100px;\">合同标的</th>");
        sb.Append("<th style=\"width:100px;\">应收房款</th>");
        sb.Append("<th style=\"width:100px;\">总收取房款</th>");
        sb.Append("<th style=\"width:100px;\">总支付房款</th>");
        sb.Append("<th style=\"width:100px;\">未付房款</th></tr>");

        double CanGetSaleMoney = 0.0;            //点数
        double GetedSaleMoney = 0.0;            //点数
        double PayedSaleMoney = 0.0;            //点数
        double RemainSaleMoney = 0.0;            //点数

        foreach (System.Data.DataRow dr in dt.Rows)
        {
            sb.Append("<tr>");
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"AreaName",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"Depart",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"UserName",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"ContractCode",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"ContractDate",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"VenderName",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"BuyerName",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"HouseAddress",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"Area",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"BargainPrice",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"CanGetSaleMoney",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"GetedSaleMoney",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"PayedSaleMoney",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"RemainSaleMoney",""));
            sb.Append("</tr>");

            CanGetSaleMoney += string.IsNullOrEmpty(dr["CanGetSaleMoney"].ToString()) ? 0 : double.Parse(dr["CanGetSaleMoney"].ToString());
            GetedSaleMoney += string.IsNullOrEmpty(dr["GetedSaleMoney"].ToString()) ? 0 : double.Parse(dr["GetedSaleMoney"].ToString());
            PayedSaleMoney += string.IsNullOrEmpty(dr["PayedSaleMoney"].ToString()) ? 0 : double.Parse(dr["PayedSaleMoney"].ToString());
            RemainSaleMoney += string.IsNullOrEmpty(dr["RemainSaleMoney"].ToString()) ? 0 : double.Parse(dr["RemainSaleMoney"].ToString());

        }
        sb.AppendFormat("<tr><td>总合计</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr></table>", CanGetSaleMoney, GetedSaleMoney, PayedSaleMoney, RemainSaleMoney);
            
        return sb.ToString();
    }

    //*******************************************
    //功能概要       ：获取房源收支平衡表
    //参数説明       ：context 请求上下文
    //返回值説明     ：程序处理信息提示
    //功能詳細       ：获取房源收支平衡表
    //作成者         ：he.jifeng
    //作成日         ：2010-09-25
    //*******************************************
    private string GetBalanceList(HttpContext context)
    {
        string strRet = string.Empty;
        string strParam = string.Empty;
        StringBuilder strHtml = new StringBuilder();

        Dictionary<string, string> dicParams = new Dictionary<string, string>();
        ContractBaseInfo bllBaseInfo = new ContractBaseInfo();

        strParam = context.Request.Params["pagesize"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("PageSize", strParam);
        }
        strParam = context.Request.Params["pageindex"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("PageIndex", strParam);
        }
        strParam = context.Request.Params["sort"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Sort", strParam);
        }
        strParam = context.Request.Params["StartTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("StartTime", strParam);
        }
        strParam = context.Request.Params["EndTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("EndTime", strParam);
        }
        strParam = context.Request.Params["Keyword"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Keyword", strParam);
        }
        strParam = context.Request.Params["BalanceStatus"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("BalanceStatus", strParam);
        }
        strParam = context.Request.Params["DateType"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("DateType", strParam);
        }

        if (!string.IsNullOrEmpty(context.Request.Params["departid"]))
        {
            dicParams.Add("DepartId", context.Request.Params["departid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["areaid"]))
        {
            dicParams.Add("AreaId", context.Request.Params["areaid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["companyid"]))
        {
            dicParams.Add("CompanyId", context.Request.Params["companyid"]);
        }
        if (!string.IsNullOrEmpty(context.Request.Params["cityid"]))
        {
            dicParams.Add("CityId", context.Request.Params["cityid"]);
        }
        else
        {
            dicParams.Add("CompanyId", CurrentUserInfo.CompanyId.ToString());
        }

        System.Data.DataSet dsBaseInfo = bllBaseInfo.GetSaleBalanceByParams(dicParams);

        strHtml.Append("{");
        strHtml.Append("success:'1',");
        strHtml.Append("msg:'',");
        strHtml.AppendFormat("data:{{{0},", JsonHelp.Convert2Json(dsBaseInfo.Tables[0]));
        strHtml.AppendFormat("count:'{0}'", dsBaseInfo.Tables[1].Rows[0][0]);
        strHtml.Append("}");
        strHtml.Append("}");

        strRet = strHtml.ToString();

        return strRet;
    }
}